/**
 * 
 */
package com.servoy.r3.tests;

import junit.framework.TestCase;

import com.servoy.r3.plugins.JRXMLSubreport;
import org.w3c.dom.*;

/**
 * @author bogdan
 * 
 */
public class JRXMLSubreportTests extends TestCase {

	private String SUBREPORT_ELEMENT_NAME = "subreport";
	
	public JRXMLSubreportTests(String name)
	{
		super(name);
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		//System.out.prntln(this.getClass().toString() + " - " + this.getName());
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public String getSubreportTextForFileName(String fn)
	{
		String subreporttext = "<subreport  isUsingCache=\"true\"> " +
		"<subreportParameter  name=\"city\"> " +
		"<subreportParameterExpression><![CDATA[$F{city}]]></subreportParameterExpression>" +
		"</subreportParameter>" +
		"<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>" +
		"<subreportExpression  class=\"java.lang.String\"><![CDATA[\"" + fn + "\"]]></subreportExpression>" +
		"</subreport>";
		
		return subreporttext;
	}
	
	public JRXMLSubreport getSubreportForFilename(String filename) throws Exception
	{
		String subreporttext = getSubreportTextForFileName(filename);
		Element subreportel = TestsUtils.getXmlElement(subreporttext, SUBREPORT_ELEMENT_NAME, 0, true);
		JRXMLSubreport subrep = new JRXMLSubreport(subreportel);
		
		return subrep;
	}

	public final void testGetSubreportFilename() throws Exception{

		String basefilename[] = {"parrepsub", "jasper", "jrxml", "test.jasper", "test.jrxml", "/test/test", "test.test/test", ""};
		
		//for(String str : basefilename)
		for (int i=0; i<basefilename.length; i++)
		{
			String str = basefilename[i];
			
			//System.out.println("getSubreportFilenameTest() for " + str);
			
			String filename1 = str + JRXMLSubreport.JRXML_JASPER_EXTENSION; 
			String result1 = getSubreportForFilename(filename1).getSubreportFilename(false);
			assertEquals(filename1, result1);
			
			String filename2 = str + JRXMLSubreport.JRXML_XML_EXTENSION; 
			String result2 = getSubreportForFilename(filename2).getSubreportFilename(false);
			assertEquals(filename2, result2);
			
			String result3 = getSubreportForFilename(filename1).getSubreportFilename(true);
			assertEquals(filename2, result3);
			
			String result4 = getSubreportForFilename(filename2).getSubreportFilename(true);
			assertEquals(filename2, result4);
		}
		
		String result5 = getSubreportForFilename("").getSubreportFilename(true);
		assertEquals("", result5);
		
		String result6 = getSubreportForFilename("").getSubreportFilename(false);
		assertEquals("", result6);
	}
	
	public final void testUpdateSubreportExpresionForXMLandSQL() throws Exception{
		
		String basefilename[] = {"", "C:\\test.jrxml", "C:\\test.jasper", "test\\file.jrxml"};
		String filenameExpected[] = {"", "test.jrxml", "test.jasper", "file.jrxml"};
		
		for(int i=0; i<basefilename.length; i++)
		{
			JRXMLSubreport subr = getSubreportForFilename(basefilename[i]);
			subr.updateSubreportExpresionForXML();
			
			String result = subr.getSubreportFilename(false);
			
			assertEquals(filenameExpected[i], result);
			
			subr.updateSubreportExpressionForSQL(basefilename[i]);
			
			result = subr.getSubreportFilename(false);
			
			assertEquals(basefilename[i], result);
		}
	}
}
